var constract = require('../../../utils/constract.js');
var app = getApp();

Page({

  data: {
    num: 0,
    date: '',
    time: '06:00',
    disabled: false,
    // startTime: '06:00',
    // endTime: '05:59'
  },

  onLoad: function (options) {
    var that = this;
    var cardId = options.cardId;
    var remindid = options.remindid;
    var type = options.type;
    that.setData({
      cardId: cardId,
      type: type
    })
    wx.getSystemInfo({
      success: function (res) {
        var abc = res.system.substr(0, 3);
        if (abc == "iOS") {
          that.setData({
            flag: true
          })
        }
      },
    });
    if(remindid) {
      app.Verification(function (token) {
        wx.request({
          url: constract.detailRemind,
          data: { id: remindid, cardId: cardId },
          header: {token: token},
          success: function (res) {
            var create_time = res.data.data.create_time.split(' ');
            var date = create_time[0];
            var time = create_time[1];
            that.setData({
              detailRemind: res.data.data,
              date: date,
              time: time,
              remindid: remindid,
              num: res.data.data.content.length
            })

          }
        })
      })
    }

    this.getDate();
  },

  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },

  getDate: function () {
    var date = new Date();
    var addMinCount = 5; //当前时间后 5 分钟
    date = date.setMinutes(date.getMinutes() + addMinCount);
    date = new Date(date);

    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hours = date.getHours();
    var minutes = date.getMinutes();
    hours = hours > 9 ? hours : "0" + hours;
    minutes = minutes > 9 ? minutes : "0" + minutes; 

    var addDayCount = 7;// 7天后
    var endDate = date.setDate(date.getDate() + addDayCount);
    endDate = new Date(endDate);
    var endYear = endDate.getFullYear();
    var endMonth = endDate.getMonth() + 1;
    var endDay = endDate.getDate();

    var start = year + '-' + month + '-' + day;
    var end = endYear + '-' + endMonth + '-' + endDay;
    var time = hours+':'+minutes;

    this.setData({
      start: start,
      end: end,
      date: start,
      time: time
    })
  },

  bindinput: function (e) {
    var txt = e.detail.value;
    this.setData({
      num: txt.length
    })
  },

  submit: function(e) {
    var len = this.data.num;
    var formId = e.detail.formId;
    var data = e.detail.value;
    var cardId = this.data.cardId;
    data.time = data.date+' '+data.time;
    data.formId = formId;
    var compareTime = data.time.replace(/-/g,'/');
    if (new Date(compareTime) < new Date()) {
      wx.showModal({
        title: '提示',
        content: '提醒时间小于当前时间',
        showCancel: false
      });
      return;
    }

    if(len < 1) {
      wx.showModal({
        title: '提示',
        content: '请输入内容',
        showCancel: false
      });
      return; 
    }
    if (wx.showLoading) {
      wx.showLoading({
        title: '保存中',
        mask: true,
      });
    }
    app.Verification(function (token) {
      wx.request({
        url: constract.addRemind,
        header: {token: token},
        data: data,
        success: function (res) {
          if (res.data.code == 12000) {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              mask: true,
              duration: 1000
            })
            setTimeout(function () {
              wx.navigateBack();
            }, 1000)
          } else {
            wx.showModal({
              title: '提示',
              content: '保存失败',
              showCancel: false
            });
            return;
          }
        },
        fail: function(){
          wx.showModal({
            title: '提示',
            content: '保存失败',
            showCancel: false
          });
          return;
        }
      })
    })
  }

})